<?php
/**
 * Index.php
 * Created by PhpStorm.
 * User: 江小波
 * Date: 2019/5/9
 * Time: 16:56
 */
namespace app\api\controller;

use app\api\open\OpenFactory;
use app\api\sign\SignFactory;
use think\controller\Rest;
use think\Request;
use think\Validate;

class Index extends Rest
{
    public function __construct()
    {
        parent::__construct();

        // 设置为api请求的唯一标识
        define('IS_OPEN_API',true);

        $request = Request::instance();

        if(!$request->isPost()){
            throw new \Exception("ILLEGAL_REQUEST");
        }
        $param = $request->post();

        if( !isset($_SERVER['HTTP_DEBUG_PASSWORD']) || $_SERVER['HTTP_DEBUG_PASSWORD'] !== 'jxb123456' ){
            $validate = new Validate([
                "method" => "require",
                "app_key" => "require",
                "sign" => "require",
                "timestamp" => "require"
            ]);
            $check = $validate->check($param);
            if($check === false){
                // 验证失败 输出错误信息
                throw new \Exception($validate->getError(),1);
            }
            // 验证请求时间
            $expired = time() - intval($param['timestamp']);
            if($expired <= 0 || $expired > 10){
                throw new \Exception("REQUEST_EXPIRED");
            }

            // 验证签名
            if( !SignFactory::createOrigin('\app\api\sign\SignDefault',$param)->check() ){
                throw new \Exception("SIGNATURE_ERROR");
            }
        }

//        $method = implode( "\\", array_map("\\app\\api\\util\\Hump::unCamel",explode(".",$param['method'])) );
        $method = array_map("\\app\\api\\util\\Hump::unCamel",explode(".",$param['method']));
        $action = lcfirst( \app\api\util\Hump::camel(array_pop($method)) );
        $api = \app\api\util\Hump::camel(array_pop($method));
        // 跳至实现类
        // call_user_func_array([$cInstance,$a],[param1,param2]);
        // call_user_func([$cInstance,$a],param1,param2);
        $result = call_user_func([
            OpenFactory::createOrigin("\\app\\api\\open\\".implode("\\",$method)."\\{$api}"),
            $action
        ],$param);

        $this->response(['code'=>200,'data'=>$result])->send();
        exit;
        /**
         * 公共请求参数
         * @apiDefine CommonApiHeader
         * @apiHeader         (HTTP)                {string}  [Client-Type=0] 调用者类型
         * @apiHeader         (HTTP)                {string}  [Version="0.0.0"] 接口版本号
         * @apiHeader         (HTTP)                {string}  [Debug=0] 接口测试
         * @apiHeader         (HTTP)                {string}  [Secret] 32位接口密码(按规则生成)
         * @apiHeader         (HTTP)                {string}  [Type='json'] 当前请求的资源类型
         * @apiHeader         (HTTP)                {string}  [Encrypted] 加密数据
         * @apiHeader         (HTTP)                {string}  [Time-Stamp] 请求时间戳
         * @apiHeaderExample  {json}                   请求头参数示例：
         *  {
         *  	"App-Mode" : "api",
         *  	// 1-h5,2-pc,3-android,4-ios,5-webapp
         *  	"Client-Type" : "1",
         *  	// 希望请求接口版本
         *  	"Version" : "0.0.0",
         *  	// 0-正式,1-调试
         *  	"Debug" : "0",
         *  	// md5加密的请求时间戳
         *  	"Secret" : "282f9135c7c8ce6512c59ff1ba49eb77",
         *  	// 希望响应的数据格式
         *  	"Type" : "json",
         *  	// 加密数据
         *  	"Encrypted" : "",
         *  	// 请求时间戳
         *  	"Time-Stamp" : "1516612829"
         *  }
         */
    }
//    public function index()
//    {
//        return 'string';
//    }
//    public function _empty($name){
//
//    }
}